package cn.m3a1.task;

import cn.m3a1.dao.UserDao;
import cn.m3a1.model.Tclass;
import cn.m3a1.model.User;
import com.iceking.dynamicweb.*;
import net.sf.json.JSONObject;

import java.util.List;

/**
 * Created by wyj on 2019-06-02.
 */
public class AdminTclass extends TaskProcessor {
    private User loginUser;
    public Boolean panduan(){
        loginUser = (User) getSession().getAttribute("loginUser");
        if (loginUser==null){
            return false;
        }

        int type=loginUser.getType();

        if (type!=99){
            return false;
        }
        return  true;
    }

    /**
     * 班级管理首页
     * @throws Exception
     */
    public void home() throws  Exception{

        if (!panduan()){
            return;
        }
        String name = getParam("name");

        FrmkView frmkView;
        UserDao userDao=new UserDao();

         int type=loginUser.getType();
        if (type==99){

            //判断是否是超级管理员
            frmkView=new FrmkView("AdminTclass.html");
            //查询所有班级信息
            getDBConn().startTransaction();
            Pagination<Tclass> pg = new Pagination<>();
            if (!GlobalUtils.isEmpty(name)&&!name.equals("0")){
                String str = new String(name.getBytes("iso8859-1"),"utf-8");
                System.out.println("name="+str);
                pg.appendWhere("where name=?",str);
            }
            getDBConn().select(Tclass.class, pg);

            List list = pg.getRows();

            //查询班主任信息
            Pagination<User> pgu = new Pagination<>();
            pgu.appendWhere("where type=?",70);
            getDBConn().select(User.class, pgu);
            List lius = pgu.getRows();
            getDBConn().stopTransaction();
            frmkView.addData("members",list);
            frmkView.addData("lius",lius);

        }else{
            //非法用户
            frmkView=new FrmkView("error.html");
        }


        frmkView.addData("loginUser",loginUser);
        toFreemarker(frmkView);
    }


    /**
     * 增加班级
     * @throws Exception
     */
    public void insert() throws  Exception{
        if (!panduan()){
            return;
        }
        JSONObject json = new JSONObject();
        String name = getParam("name");
        int time = getParamInt("time");
        int managerid = getParamInt("managerid");
        //判断参数是否为空
        if (GlobalUtils.isEmpty(name) || time==0||managerid==0) {
            json.put("succ", false);
            json.put("stmt", "参数不全，创建失败");
            toJson(json.toString());
            return;
        }
        Tclass tclass=new Tclass();
        tclass.setManagerid(managerid);
        tclass.setName(name);
        tclass.setYear(time);
        getDBConn().save(tclass);

    }


    /**
     * 根据id删除数据
     * @throws Exception
     */
    public void del() throws Exception {
        if (!panduan()){
            return;
        }
        int id = getParamInt("id");
        System.out.println("del"+id);
        if (id>1){
            Tclass tclass = getDBConn().findByPK(Tclass.class, id);
            getDBConn().delete(tclass);
        }
    }


    /**
     * 管理员修改页面
     *
     * @throws Exception
     */
    public void update() throws Exception {
        if (!panduan()){
            return;
        }
        JSONObject json = new JSONObject();
        Tclass tclass=new Tclass();
        tclass.setId(getParamInt("id"));
        Pagination<Tclass> pg = new Pagination<>();
        pg.appendWhere("where id=?", tclass.getId());
        getDBConn().select(Tclass.class, pg);

        tclass = pg.getFirstResult();
        tclass.toString();
        tclass.setName(getParam("name"));
        tclass.setYear(getParamInt("year"));
        tclass.setManagerid(getParamInt("managerid"));
        getDBConn().save(tclass);
        json.put("succ", true);
        json.put("stmt", "更改成功");
        toJson(json.toString());
    }

    public void findTclass() throws Exception {
        if (!panduan()){
            return;
        }
        JSONObject json = new JSONObject();

        int id = getParamInt("id");
        System.out.println("id=" + id);
        //根据用户名和密码去进行数据库访问
        Pagination<Tclass> pg = new Pagination<>();
        pg.appendWhere("where id=?", id);
        getDBConn().select(Tclass.class, pg);
        Tclass tclass = pg.getFirstResult();
        if (tclass != null) {

            json.put("succ", true);
            json.put("tclass", tclass);
            toJson(json.toString());
        }
    }

}
